<?php

namespace plugin\ycMassage\app\api\controller;

use app\common\service\UploadService;
use plugin\ycMassage\utils\enum\State;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageCollectTechnician;
use plugin\ycMassage\app\model\MassageMoments;
use plugin\ycMassage\app\model\MassageMomentsLike;
use plugin\ycMassage\utils\enum\Examine;
use plugin\ycMassage\utils\enum\OrdersState;
use support\Request;
use think\facade\Db;

class MomentsController extends BaseController
{
    protected $noNeedLogin = ["list"];
    public function list(Request $request)
    {
        $saas_appid = $request->appid;
        if ($request->saas_appid) {
            $saas_appid = $request->saas_appid;
        }
        $limit = $request->get('limit', 10);
        $page = $request->get('page', 1);
        $where = [
            ['m.saas_appid', '=', $saas_appid]
        ];
        $technician_id = $request->get('technician_id');
        $action = $request->get('action');
        switch ($action) {
            case 'technician':
                $where[] = ['m.uid', '=', $request->uid];
                break;
            default:
                $where[]=['m.examine', '=', Examine::YES['value']];
                if ($technician_id) {
                    $where[] = ['m.uid', '=', $technician_id];
                }
                if ($technician_id) {
                    if (!MassageCollectTechnician::where(['technician_id' => $technician_id, 'uid' => $request->uid])->count()) {
                        $where[] = ['m.view_role', '=', 'public'];
                    }
                } else {
                    $view_role = $request->get('view_role', 'public');
                    switch ($view_role) {
                        case 'public':
                            $where[] = ['m.view_role', '=', 'public'];
                            if ($request->city) {
                                $where[] = ['t.city_id', '=', $request->city];
                            } elseif ($request->province) {
                                $where[] = ['t.province_id', '=', $request->province];
                            }
                            break;
                        case 'fans':
                            if (!$request->uid) {
                                return $this->fail('请先登录');
                            }
                            $collect = MassageCollectTechnician::where(['saas_appid' => $saas_appid, 'uid' => $request->uid])->column('technician_id');
                            $where[] = ['m.uid', 'in', $collect];
                            break;
                    }
                }
                break;
        }
        $List = MassageMoments::alias('m')->where($where)
            ->join('massage_user u', 'u.id=m.uid')
            ->join('massage_user_technician t', 't.uid=m.uid')
            ->field('m.*,u.nickname,u.headimg')
            ->order('m.create_time desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) use ($request) {
                $item->is_collect = 0;
                $item->is_like = 0;
                $item->like_list = [];
                if ($request->uid) {
                    $item->is_collect = MassageCollectTechnician::where(['technician_id' => $item->uid, 'uid' => $request->uid])->count();
                    $item->is_like = MassageMomentsLike::where(['moments_id' => $item->id, 'uid' => $request->uid])->count();
                }
                $item->like_list = MassageMomentsLike::alias('mk')->where(['mk.moments_id' => $item->id])->join('massage_user u', 'u.id=mk.uid')->field('mk.uid,u.nickname')->select();
                $item->examine_text=Examine::getText($item->examine)['text'];
            });
        if ($List->isEmpty()) {
            return $this->fail('暂无记录');
        }
        return $this->successRes($List);
    }
    public function release(Request $request)
    {
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $content = $request->post('content');
        $images = $request->post('images');
        $video = $request->post('video');
        if (empty($content) && empty($images) && empty($video)) {
            return $this->fail('请填写内容或上传图片或上传视频');
        }
        $view_role = $request->post('view_role');
        $MassageMoments = new MassageMoments();
        $MassageMoments->uid = $uid;
        $MassageMoments->saas_appid = $saas_appid;
        $MassageMoments->content = $content;
        $MassageMoments->video = $video;
        $MassageMoments->images = $images;
        $MassageMoments->view_role = $view_role;
        $MassageMoments->examine = getHpConfig('moments_examine', $saas_appid, 'technician');
        if (!$MassageMoments->examine) {
            $MassageMoments->examine = Examine::WAIT['value'];
        }
        if ($MassageMoments->save()) {
            return $this->success('发布成功');
        }
        return $this->fail('发布失败');
    }
    public function like(Request $request)
    {
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $id = $request->post('id');
        $moments = MassageMoments::where(['id' => $id, 'saas_appid' => $saas_appid])->find();
        if (!$moments) {
            return $this->fail('动态已不存在');
        }
        $state = 1;
        Db::startTrans();
        try {
            $like = MassageMomentsLike::where(['moments_id' => $id, 'uid' => $uid])->find();
            if ($like) {
                $like->delete();
                $moments->like_num = Db::raw('like_num-1');
                $moments->save();
                $state = 0;
            } else {
                $like = new MassageMomentsLike();
                $like->saas_appid = $saas_appid;
                $like->moments_id = $id;
                $like->uid = $uid;
                $like->technician_id = $moments->uid;
                $like->save();
                $moments->like_num = Db::raw('like_num+1');
                $moments->save();
            }
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            return $this->fail('操作失败');
        }
        return $this->successRes(['state' => $state]);
    }
    public function delete(Request $request)
    {
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $id = $request->post('id');
        $moments = MassageMoments::where(['id' => $id, 'uid' => $uid, 'saas_appid' => $saas_appid])->find();
        if (!$moments) {
            return $this->fail('动态不存在');
        }
        Db::startTrans();
        try {
            $moments->delete();
            MassageMomentsLike::where(['moments_id' => $moments->id])->delete();
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            return $this->fail('删除失败');
        }
        return $this->success('已删除');
    }
}
